package com.zhiyou100.controller.house;

import com.zhiyou100.dao.Impl.HouseDaoImpl;
import com.zhiyou100.emtity.House;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * @description:
 * @author: 马跃
 * @create: 2021-01-22 19:02
 **/
@WebServlet("/houseUpdate")
public class HouseUpdateServlet extends HttpServlet {

    private House house;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        try {
            BeanUtils.populate(house, request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        new HouseDaoImpl().update(house);
        request.getRequestDispatcher("/houseQueryALLL").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        int id = Integer.parseInt(request.getParameter("id"));
        house = new HouseDaoImpl().query(id);
        request.getSession().setAttribute("house", house);
        request.getRequestDispatcher("./house/houseEdit.jsp").forward(request, response);
    }
}
